package com.bdqn.bookshop.common;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;

import java.util.Map;
import java.util.Random;


public class LoginPhoneConstants {


    //产品名称:云通信短信API产品,开发者无需替换
    public  static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    public  static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
   public static final  String accessKeyId = "LTAI4GGSJYBc4K3MEVENyHtX";  // TODO 修改成自己的
    public  static final String accessKeySecret = "e8dpKXXlfZaMH7u6qhskEWp4obvLZm";   // TODO 修改成自己的



    public static String SendSmsResponse(String phone) throws ClientException {
        String result = "";
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化ascClient需要的几个参数
        //初始化ascClient,暂时不支持多region（请勿修改）
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
                accessKeySecret);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "小鹏通信");
        request.putQueryParameter("TemplateCode", "SMS_199217328");
        request.putQueryParameter("TemplateParam","{\"code\":\""+getMsgCode()+"\"}");



    try{
        //请求失败这里会抛ClientException异常
        CommonResponse response = acsClient.getCommonResponse(request);
        System.out.println(response.getData());
        String infojson = response.getData();
        Gson gson2 = new Gson();
        Map map = gson2.fromJson(infojson, Map.class);
        String codes = map.get("Message").toString();
        System.out.println("codes="+codes);
        if(codes.equals("OK")){
            result="ok";
        }else {
            result="not_ok";
        }
    } catch (ServerException e) {
        e.printStackTrace();
    } catch (ClientException e) {
        e.printStackTrace();
    }
        System.out.println("result="+result);
        return result;
    }


    /**
     * 验证码生成
     * @return
     */
    private static String getMsgCode() {
            int n = 6;
            StringBuilder code = new StringBuilder();
            Random ran = new Random();
            for (int i = 0; i < n; i++) {
                code.append(Integer.valueOf(ran.nextInt(10)).toString());
            }
            return code.toString();

        }
    }
